#ifndef UTILS_H
#define UTILS_H
#include <QtCore>
#include <chrono>
#include <string>
#include <iostream>
#include <sstream>
#include <iomanip>
#include <thread>

class Utils
{
public:
    Utils();

    static QString formatTime(long ms)
    {
        int ss = 1000;
        int mi = ss * 60;
        int hh = mi * 60;
        int dd = hh * 24;

        long day = ms / dd;
        long hour = (ms - day * dd) / hh;
        long minute = (ms - day * dd - hour * hh) / mi;
        long second = (ms - day * dd - hour * hh - minute * mi) / ss;
        long milliSecond = ms - day * dd - hour * hh - minute * mi - second * ss;

        QString hou = QString::number(hour,10);
        QString min = QString::number(minute,10);
        QString sec = QString::number(second,10);
        QString msec = QString::number(milliSecond,10);

        //qDebug() << "minute:" << min << "second" << sec << "ms" << msec <<endl;
        QString res = "";
        if(hour < 10)
        {
            res.append("0").append(hou);
        }
        else
        {
            res.append(hou);
        }

        if(minute < 10)
        {
            res.append(":0").append(min);
        }
        else {
            res.append(":").append(min);
        }

        if(second < 10)
        {
            res.append(":0").append(sec);
        }
        else
        {
            res.append(":").append(sec);
        }

        return res ;
    }

    static QString formatFloat(float val)
    {
        QString str;
        if(!std::isnan(val))
        {
            str = QString::number(val, 'f', 3);
        }
        else
        {
            str = QString::number(0, 'f', 3);
        }

        return str;
    }

    static std::string getCurrentTime()
    {
        auto tt = std::chrono::system_clock::to_time_t(std::chrono::system_clock::now());
        struct tm* ptm = localtime(&tt);
        char date[60] = {0};
        sprintf(date, "%d-%02d-%02d %02d:%02d:%02d",
            (int)ptm->tm_year + 1900,(int)ptm->tm_mon + 1,(int)ptm->tm_mday,
            (int)ptm->tm_hour,(int)ptm->tm_min,(int)ptm->tm_sec);
        return std::string(date);
    }

    static std::string getCurrentDayTime()
    {
        auto tt = std::chrono::system_clock::to_time_t(std::chrono::system_clock::now());
        struct tm* ptm = localtime(&tt);
        char date[60] = {0};
        sprintf(date, "%02d-%02d-%02d",
            (int)ptm->tm_hour,(int)ptm->tm_min,(int)ptm->tm_sec);
        return std::string(date);
    }

    //获取当前日期
    static std::string getCurrentDate()
    {
        auto tt = std::chrono::system_clock::to_time_t(std::chrono::system_clock::now());
        struct tm* ptm = localtime(&tt);
        char date[60] = {0};
        sprintf(date, "%d-%02d-%02d",
            (int)ptm->tm_year + 1900,(int)ptm->tm_mon + 1,(int)ptm->tm_mday);
        return std::string(date);
    }


    static void delay(int millsec)
    {
        std::this_thread::sleep_for(std::chrono::milliseconds(millsec));
    }

    static QString formatCommand(const QString& cmd, const float& val)
    {
        QString result;
        QString strVal;
        if(!std::isnan(val))
        {
            float pos = std::fabs(val);

            std::cout << "=====================";
            strVal = QString::number(pos, 'e', 6);
            strVal.insert(strVal.size()-1, "0");
            strVal.insert(strVal.size()-1, "0");

            if(val < 0.0)
            {
                strVal = strVal.replace(0, 0, "-");
            }
            else
            {
                strVal = strVal.replace(0, 0, "+");
            }
        }
        else
        {
            std::cout << "+++++++++++++++++";
            strVal = QString::number(0, 'e', 6);
            strVal.insert(strVal.size()-1, "0");
            strVal.insert(strVal.size()-1, "0");
            strVal = QString("+").append(strVal);
        }
        result = QString("{%1[%2]}").arg(cmd).arg(strVal);

        return result;
    }

    static bool isFileExist(const QString& fullFileName)
    {
        QFileInfo fileInfo(fullFileName);
        if(fileInfo.isFile())
        {
            return true;
        }
        return false;
    }
};

#endif // UTILS_H
